package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.DeliveryPlace;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class DeliveryPlaceService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieces the session (connection).
     */
    public DeliveryPlaceService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds DeliveryPlace to the db.
     * @param dp DeliveryPlace to be inserted
     * @return ID of the added DeliveryPlace
     */
    public int addDeliveryPlace(DeliveryPlace dp) {
        session.beginTransaction();
        int id = getMaxDeliveryPlaceID() + 1;
        dp.setDeliveryPlaceID(id);
        id = (Integer) session.save(dp);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of DeliveryPlaces.
     * @return Max ID
     */
    public int getMaxDeliveryPlaceID() {
        String hql = "select max(d.deliveryPlaceID) from DeliveryPlace d";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxdpid = (Integer) list.get(0);
            return maxdpid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a DeliveryPlace identified by the ID.
     * @param id of the DeliveryPlace
     * @return DeliveryPlace with the ID given (null if not found)
     */
    public DeliveryPlace getDeliveryPlaceByID(int id) {
        session.beginTransaction();
        DeliveryPlace dp = (DeliveryPlace) session.get(DeliveryPlace.class, new Integer(id));
        session.getTransaction().commit();
        return (dp);
    }

    /**
     * Retrieves a list of all DeliveryPlaces stored in the db.
     * @return List of all DeliveryPlaces (null if no DeliveryPlaces)
     */
    public List<DeliveryPlace> getAllDeliveryPlaces() {
        session.beginTransaction();
        List<DeliveryPlace> dpList = session.createQuery("from DeliveryPlace").list();
        session.getTransaction().commit();
        return (dpList);
    }

    /**
     * Updates a specified DeliveryPlace identified by ID.
     * @param newDeliveryPlace an updated DeliveryPlace
     */
    public void updateDeliveryPlace(DeliveryPlace newDeliveryPlace) {
        session.beginTransaction();
        DeliveryPlace dp = (DeliveryPlace) session.get(DeliveryPlace.class, new Integer(newDeliveryPlace.getDeliveryPlaceID()));
        dp.setStreet(newDeliveryPlace.getStreet());
        dp.setStreetNo(newDeliveryPlace.getStreetNo());
        dp.setCity(newDeliveryPlace.getCity());
        dp.setZip(newDeliveryPlace.getZip());
        dp.setHomeDelivery(newDeliveryPlace.isHomeDelivery());
        dp.setOffers(newDeliveryPlace.getOffers());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the DeliveryPlace from db.
     * @param id DeliveryPlace ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteDeliveryPlace(int id) {
        try {
            session.beginTransaction();
            DeliveryPlace dp = (DeliveryPlace) session.get(DeliveryPlace.class, new Integer(id));
            session.delete(dp);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}